package org.example;

import java.sql.*;
import java.text.MessageFormat;
import java.util.Scanner;

public class Demo01_DriverManager {

    public static void main(String[] args) {
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;

        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            //try {
                connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/java113?characterEncoding=utf8" +
                        "&allowPublicKeyRetrieval=true&useSSL=false", "root", "liangzai666");
                statement = connection.createStatement();
                System.out.println("请输入学生姓名：");
                Scanner scanner = new Scanner(System.in);
                String name = scanner.nextLine();
                String sql = "select student_id, sn, name, mail, class_id from student where name = '"+name+"'";
                resultSet = statement.executeQuery(sql);
                while (resultSet.next()) {
                    Long stuId = resultSet.getLong(1);
                    String sn = resultSet.getString(2);
                    String stuName = resultSet.getString(3);
                    String mail = resultSet.getString(4);
                    Long clId = resultSet.getLong(5);
                    System.out.println(MessageFormat.format("学生编号={0}, 学号={1}, 学生姓名={2}, 邮箱={3}, 班级编号={4}", stuId, sn,
                            stuName, mail, clId));
                }
//            } catch (SQLException e) {
//                throw new RuntimeException(e);
//            }
        }catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
//        } catch (ClassNotFoundException e) {
//            throw new RuntimeException(e);
//        }catch (SQLException e) {
//            throw new RuntimeException(e);
//        }
        } finally {
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}
